var express = require("express");
var sequelize= require("../utils/sequelizeUtil")

var router = express.Router();
var resultModule=require("../resultModels/AliGenieResultModule")
var models=require("../models")

router.get("/", function(req, res, next) {
	console.log(req.query)
	console.log(req.body)
  res.render("index", { title: "Express" });
});

router.post("/", function(req, res, next) {


	var reqData=req.body
	var slotEntities=reqData.slotEntities
	var channelName=""
	console.log(slotEntities)
	for(var i=0;i<slotEntities.length;i++){
		if (slotEntities[i].intentParameterId==10038) {
			channelName=slotEntities[i].originalValue
		}
	}
	if (channelName!="") {
		console.log(channelName)

		models.Feed.findOne(
			{ where : {title:channelName}})
			.then(item=>{
				// console.log(item)
				models.UserEntry.findAndCountAll({where:{feed_id:item.dataValues.id},include:[{model:models.Entry}]
						,limit:5}
				    )
				  .then(result => {
				  	console.log("result")
				  	console.log(result)
				  	var resultJson=resultModule.getSuccessResult()
				  	resultJson.returnValue.reply=result.rows[0].ttrss_entry.dataValues.title
					resultStr=JSON.stringify(resultJson)
				  	res.end(resultStr)
				  })
			})


	}

});



router.route("/getFeedList")
	.get(getFeedList)
	.post(getFeedList);

function getFeedList(req,res,next){
	console.log(req.url)

	var reqData=req.body
	var slotEntities=reqData.slotEntities
	var channelName=""
	// for(var i=0;i<slotEntities.length;i++){
	// 	if (slotEntities[i].intentParameterId==10038) {
	// 		channelName=slotEntities[i].originalValue
	// 	}
	// }
	// if (!isEmpty(channelName)) {
	// 	console.log(channelName)
	// 	var feeds = new Feeds(sequelize)	
	// 	feeds.findOne({ where : {title:channelName}})
	// 		.then(item=>{
	// 			console.log(item)
	// 		})
	// }
	
	// feeds.findOne({ where : {title}})
 //    feeds.findAll().then(feedList=>{
 //    		for (var i = 0; i < feedList.length; i++) {
 //    			console.log(feedList[i].dataValues)
 //    		}
 //    	});

 	// var userEntity = new UserEntity(sequelize)
 	// userEntity.findAndCountAll({
	 //     offset: 0,
	 //     limit: 20
	 //  })
	 //  .then(result => {
	 //    console.log(result.count);
	 //    console.log(result.rows);
	 //  });

	res.end("hello")
}

module.exports = router;



